#!/bin/bash

_loss_seven_ago=0
_loss_seven_begin=0
_loss_seven_end=0

_loss_fourteen_ago=0
_loss_fourteen_begin=0
_loss_fourteen_end=0

_loss_thirty_ago=0
_loss_thirty_begin=0
_loss_thirty_end=0

_retention_one_day_ago=0
_retention_one_day_begin=0
_retention_one_day_end=0

_retention_two_day_ago=0
_retention_two_day_begin=0
_retention_two_day_end=0

_retention_three_day_ago=0
_retention_three_day_begin=0
_retention_three_day_end=0

_retention_four_day_ago=0
_retention_four_day_begin=0
_retention_four_day_end=0

_retention_five_day_ago=0
_retention_five_day_begin=0
_retention_five_day_end=0

_retention_six_day_ago=0
_retention_six_day_begin=0
_retention_six_day_end=0

_retention_seven_day_ago=0
_retention_seven_day_begin=0
_retention_seven_day_end=0

_retention_one_week_ago=0
_retention_one_week_begin=0
_retention_one_week_end=0

_retention_two_week_ago=0
_retention_two_week_begin=0
_retention_two_week_end=0

_retention_three_week_ago=0
_retention_three_week_begin=0
_retention_three_week_end=0

_retention_one_month_ago=0
_retention_one_month_begin=0
_retention_one_month_end=0

_retention_two_month_ago=0
_retention_two_month_begin=0
_retention_two_month_end=0

_retention_three_month_ago=0
_retention_three_month_begin=0
_retention_three_month_end=0

_current_day=$(date +%Y%m%d -d "-1 day")
if [[ $# -eq 1 ]]
then
	_current_day=$1
fi
_loss_seven_ago=$(date +%Y%m%d -d "-7 day $_current_day")
_loss_seven_begin=$(date +%Y%m%d -d "-6 day $_current_day")
_loss_seven_end=$(date +%Y%m%d -d "-0 day $_current_day")


_loss_fourteen_ago=$(date +%Y%m%d -d "-14 day $_current_day")
_loss_fourteen_begin=$(date +%Y%m%d -d "-13 day $_current_day")
_loss_fourteen_end=$(date +%Y%m%d -d "-0 day $_current_day")


_loss_thirty_ago=$(date +%Y%m%d -d "-30 day $_current_day")
_loss_thirty_begin=$(date +%Y%m%d -d "-29 day $_current_day")
_loss_thirty_end=$(date +%Y%m%d -d "-0 day $_current_day")


_retention_one_day_ago=$(date +%Y%m%d -d "-1 day $_current_day")
_retention_one_day_begin=$(date +%Y%m%d -d "-0 day $_current_day")
_retention_one_day_end=$(date +%Y%m%d -d "-0 day $_current_day")

_retention_two_day_ago=$(date +%Y%m%d -d "-2 day $_current_day")
_retention_two_day_begin=$(date +%Y%m%d -d "-0 day $_current_day")
_retention_two_day_end=$(date +%Y%m%d -d "-0 day $_current_day")

_retention_three_day_ago=$(date +%Y%m%d -d "-3 day $_current_day")
_retention_three_day_begin=$(date +%Y%m%d -d "-0 day $_current_day")
_retention_three_day_end=$(date +%Y%m%d -d "-0 day $_current_day")

_retention_four_day_ago=$(date +%Y%m%d -d "-4 day $_current_day")
_retention_four_day_begin=$(date +%Y%m%d -d "-0 day $_current_day")
_retention_four_day_end=$(date +%Y%m%d -d "-0 day $_current_day")

_retention_five_day_ago=$(date +%Y%m%d -d "-5 day $_current_day")
_retention_five_day_begin=$(date +%Y%m%d -d "-0 day $_current_day")
_retention_five_day_end=$(date +%Y%m%d -d "-0 day $_current_day")

_retention_six_day_ago=$(date +%Y%m%d -d "-6 day $_current_day")
_retention_six_day_begin=$(date +%Y%m%d -d "-0 day $_current_day")
_retention_six_day_end=$(date +%Y%m%d -d "-0 day $_current_day")

_retention_seven_day_ago=$(date +%Y%m%d -d "-7 day $_current_day")
_retention_seven_day_begin=$(date +%Y%m%d -d "-0 day $_current_day")
_retention_seven_day_end=$(date +%Y%m%d -d "-0 day $_current_day")

_retention_eight_day_ago=$(date +%Y%m%d -d "-8 day $_current_day")
_retention_eight_day_begin=$(date +%Y%m%d -d "-0 day $_current_day")
_retention_eight_day_end=$(date +%Y%m%d -d "-0 day $_current_day")

_retention_nine_day_ago=$(date +%Y%m%d -d "-9 day $_current_day")
_retention_nine_day_begin=$(date +%Y%m%d -d "-0 day $_current_day")
_retention_nine_day_end=$(date +%Y%m%d -d "-0 day $_current_day")

_retention_one_week_ago_1=$(date +%Y%m%d -d "-13 day $_current_day")
_retention_one_week_ago_2=$(date +%Y%m%d -d "-7 day $_current_day")
_retention_one_week_begin=$(date +%Y%m%d -d "-6 day $_current_day")
_retention_one_week_end=$(date +%Y%m%d -d "-0 day $_current_day")

_retention_two_week_ago_1=$(date +%Y%m%d -d "-20 day $_current_day")
_retention_two_week_ago_2=$(date +%Y%m%d -d "-14 day $_current_day")
_retention_two_week_begin=$(date +%Y%m%d -d "-6 day $_current_day")
_retention_two_week_end=$(date +%Y%m%d -d "-0 day $_current_day")


_retention_three_week_ago_1=$(date +%Y%m%d -d "-27 day $_current_day")
_retention_three_week_ago_2=$(date +%Y%m%d -d "-21 day $_current_day")
_retention_three_week_begin=$(date +%Y%m%d -d "-6 day $_current_day")
_retention_three_week_end=$(date +%Y%m%d -d "-0 day $_current_day")


_retention_four_week_ago_1=$(date +%Y%m%d -d "-34 day $_current_day")
_retention_four_week_ago_2=$(date +%Y%m%d -d "-28 day $_current_day")
_retention_four_week_begin=$(date +%Y%m%d -d "-6 day $_current_day")
_retention_four_week_end=$(date +%Y%m%d -d "-0 day $_current_day")


_retention_five_week_ago_1=$(date +%Y%m%d -d "-41 day $_current_day")
_retention_five_week_ago_2=$(date +%Y%m%d -d "-35 day $_current_day")
_retention_five_week_begin=$(date +%Y%m%d -d "-6 day $_current_day")
_retention_five_week_end=$(date +%Y%m%d -d "-0 day $_current_day")


_retention_six_week_ago_1=$(date +%Y%m%d -d "-48 day $_current_day")
_retention_six_week_ago_2=$(date +%Y%m%d -d "-42 day $_current_day")
_retention_six_week_begin=$(date +%Y%m%d -d "-6 day $_current_day")
_retention_six_week_end=$(date +%Y%m%d -d "-0 day $_current_day")


_retention_seven_week_ago_1=$(date +%Y%m%d -d "-55 day $_current_day")
_retention_seven_week_ago_2=$(date +%Y%m%d -d "-49 day $_current_day")
_retention_seven_week_begin=$(date +%Y%m%d -d "-6 day $_current_day")
_retention_seven_week_end=$(date +%Y%m%d -d "-0 day $_current_day")


_retention_eight_week_ago_1=$(date +%Y%m%d -d "-62 day $_current_day")
_retention_eight_week_ago_2=$(date +%Y%m%d -d "-56 day $_current_day")
_retention_eight_week_begin=$(date +%Y%m%d -d "-6 day $_current_day")
_retention_eight_week_end=$(date +%Y%m%d -d "-0 day $_current_day")


_retention_nine_week_ago_1=$(date +%Y%m%d -d "-69 day $_current_day")
_retention_nine_week_ago_2=$(date +%Y%m%d -d "-63 day $_current_day")
_retention_nine_week_begin=$(date +%Y%m%d -d "-6 day $_current_day")
_retention_nine_week_end=$(date +%Y%m%d -d "-0 day $_current_day")

_retention_one_month_ago_1=$(date +%Y%m -d "-1 month $_current_day")"01"
_year=$(date +%Y -d "-1 month $_current_day")
_month=$(date +%m -d "-1 month $_current_day")
_retention_one_month_ago_2=${_year}${_month}$(cal $_month $_year | xargs | awk '{print $NF}')
_retention_one_month_begin=$(date +%Y%m -d "-0 month $_current_day")"01"
_year=$(date +%Y -d "-0 month $_current_day")
_month=$(date +%m -d "-0 month $_current_day")
_retention_one_month_end=${_year}${_month}$(cal $_month $_year | xargs | awk '{print $NF}')

_retention_two_month_ago_1=$(date +%Y%m -d "-2 month $_current_day")"01"
_year=$(date +%Y -d "-2 month $_current_day")
_month=$(date +%m -d "-2 month $_current_day")
_retention_two_month_ago_2=${_year}${_month}$(cal $_month $_year | xargs | awk '{print $NF}')
_retention_two_month_begin=$(date +%Y%m -d "-1 month $_current_day")"01"
_year=$(date +%Y -d "-0 month $_current_day")
_month=$(date +%m -d "-0 month $_current_day")
_retention_two_month_end=${_year}${_month}$(cal $_month $_year | xargs | awk '{print $NF}')

_retention_three_month_ago_1=$(date +%Y%m -d "-3 month $_current_day")"01"
_year=$(date +%Y -d "-3 month $_current_day")
_month=$(date +%m -d "-3 month $_current_day")
_retention_three_month_ago_2=${_year}${_month}$(cal $_month $_year | xargs | awk '{print $NF}')
_retention_three_month_begin=$(date +%Y%m -d "-2 month $_current_day")"01"
_year=$(date +%Y -d "-0 month $_current_day")
_month=$(date +%m -d "-0 month $_current_day")
_retention_three_month_end=${_year}${_month}$(cal $_month $_year | xargs | awk '{print $NF}')

source ~/.bashrc
source /etc/profile
result=`hdfs dfs -ls /user/hive/warehouse/pass_data.db/user_login/ | awk -F"dt=" '{print $2}'`
for r in ${result[0]}
do
	if [[ $r -ge $_retention_nine_week_ago_1 && $r -le $_current_day ]]
	then
		input="/user/hive/warehouse/pass_data.db/user_login/dt=$r,/user/hive/warehouse/pass_data.db/user_reg/dt=$r,"$input
		#input="/user/hive/warehouse/pass_data.db/user_login/dt=$r,"$input
	fi
done
input=${input:0:${#input}-1}

result=$(hdfs dfs -rm -r /tmp/usermodel_test/${_current_day}*)

echo "======================$_current_day========================="

hadoop jar all_0.3.jar com.baidu.user.UserModel_Statistic \
_current_day $_current_day $_current_day \
_loss_seven_ago $_loss_seven_ago $_loss_seven_ago \
_loss_seven $_loss_seven_begin $_loss_seven_end \
_loss_fourteen_ago $_loss_fourteen_ago $_loss_fourteen_ago \
_loss_fourteen $_loss_fourteen_begin $_loss_fourteen_end \
_loss_thirty_ago $_loss_thirty_ago $_loss_thirty_ago \
_loss_thirty $_loss_thirty_begin $_loss_thirty_end \
_retention_one_day_ago $_retention_one_day_ago $_retention_one_day_ago \
_retention_one_day $_retention_one_day_begin $_retention_one_day_end \
_retention_two_day_ago $_retention_two_day_ago $_retention_two_day_ago \
_retention_two_day $_retention_two_day_begin $_retention_two_day_end \
_retention_three_day_ago $_retention_three_day_ago $_retention_three_day_ago \
_retention_three_day $_retention_three_day_begin $_retention_three_day_end \
_retention_four_day_ago $_retention_four_day_ago $_retention_four_day_ago \
_retention_four_day $_retention_four_day_begin $_retention_four_day_end \
_retention_five_day_ago $_retention_five_day_ago $_retention_five_day_ago \
_retention_five_day $_retention_five_day_begin $_retention_five_day_end \
_retention_six_day_ago $_retention_six_day_ago $_retention_six_day_ago \
_retention_six_day $_retention_six_day_begin $_retention_six_day_end \
_retention_seven_day_ago $_retention_seven_day_ago $_retention_seven_day_ago \
_retention_seven_day $_retention_seven_day_begin $_retention_seven_day_end \
_retention_eight_day_ago $_retention_eight_day_ago $_retention_eight_day_ago \
_retention_eight_day $_retention_eight_day_begin $_retention_eight_day_end \
_retention_nine_day_ago $_retention_nine_day_ago $_retention_nine_day_ago \
_retention_nine_day $_retention_nine_day_begin $_retention_nine_day_end \
_retention_one_week_ago $_retention_one_week_ago_1 $_retention_one_week_ago_2 \
_retention_one_week $_retention_one_week_begin $_retention_one_week_end \
_retention_two_week_ago $_retention_two_week_ago_1 $_retention_two_week_ago_2 \
_retention_two_week $_retention_two_week_begin $_retention_two_week_end \
_retention_three_week_ago $_retention_three_week_ago_1 $_retention_three_week_ago_2 \
_retention_three_week $_retention_three_week_begin $_retention_three_week_end \
_retention_four_week_ago $_retention_four_week_ago_1 $_retention_four_week_ago_2 \
_retention_four_week $_retention_four_week_begin $_retention_four_week_end \
_retention_five_week_ago $_retention_five_week_ago_1 $_retention_five_week_ago_2 \
_retention_five_week $_retention_five_week_begin $_retention_five_week_end \
_retention_six_week_ago $_retention_six_week_ago_1 $_retention_six_week_ago_2 \
_retention_six_week $_retention_six_week_begin $_retention_six_week_end \
_retention_seven_week_ago $_retention_seven_week_ago_1 $_retention_seven_week_ago_2 \
_retention_seven_week $_retention_seven_week_begin $_retention_seven_week_end \
_retention_eight_week_ago $_retention_eight_week_ago_1 $_retention_eight_week_ago_2 \
_retention_eight_week $_retention_eight_week_begin $_retention_eight_week_end \
_retention_nine_week_ago $_retention_nine_week_ago_1 $_retention_nine_week_ago_2 \
_retention_nine_week $_retention_nine_week_begin $_retention_nine_week_end \
-input $input \
-output1 /tmp/usermodel_test/${_current_day}_1 \
-output2 /tmp/usermodel_test/${_current_day}_2

#echo $result
